import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Main3 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入修改的题目的名称");
        String name = scanner.next();
        System.out.println("请输入新的题目的内容");
        String desc = scanner.next();

        //创建初始化数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/subjectlyw?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");
        //构造连接
        Connection connection = dataSource.getConnection();
        //构造sql
        String sql = "update subject set content = \'" + desc + "\'" + " where name = \'" + name + "\'";
        PreparedStatement statement = connection.prepareStatement(sql);
        //执行sql
        int flag = statement.executeUpdate(sql);
        if (flag != 0) {
            System.out.println("修改成功!");
        } else {
            System.out.println("修改失败,当前题目不存在");
        }
        statement.close();
        connection.close();
    }
}
